package com.dsh.authorityManagement.controller;

import com.dsh.authorityManagement.entity.AdminPermission;
import com.dsh.authorityManagement.entity.RoleModel;
import com.dsh.authorityManagement.entity.vo.PermissionMenuVo;
import com.dsh.authorityManagement.entity.vo.UserForPermissionVo;
import com.dsh.authorityManagement.entity.vo.UserIdAndPermissionIdVo;
import com.dsh.authorityManagement.service.AdminPermissionService;
import com.dsh.model.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "用户权限管理")
@RestController
@RequestMapping("permission")
public class AdminPermissionController {
    @Autowired
    AdminPermissionService adminPermissionService;
    @ApiOperation("查询权限树")
    @GetMapping("selectPermissionTree")
    public Result selectPermissionTree(){
        List<AdminPermission> adminPermissions = adminPermissionService.selectPermissionTree();
        return Result.success(adminPermissions,"");
    }
    @ApiOperation("查询所有的角色")
    @GetMapping("selectAllRole")
    public Result selectAllRole(){
        List<RoleModel> roleModels = adminPermissionService.selectAllRole();
        return Result.success(roleModels,"");
    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "userId", value = "人员的id", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "storeId", value = "机构id", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "roleId", value = "角色id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "permissionIds", value = "权限ID数组", dataType = "int", paramType = "body", allowMultiple = true, required = true )    })
    @ApiOperation("添加用户所在机构的角色对应的权限")
    @PostMapping("insertPermissionForRoleAndStoreAndAdmin")
    public Result insertPermissionForRoleAndStoreAndAdmin(@RequestBody Integer[] permissionIds, int userId, int storeId, int roleId ){
        this.adminPermissionService.insertPermissionForRoleAndStoreAndAdmin( permissionIds,userId,storeId,roleId);
        return Result.success("");
    }
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="query", name = "userId", value = "人员的id", required = true, dataType = "int"),
//            @ApiImplicitParam(paramType="query", name = "permissionId", value = "权限ID数组", required = true, dataType = "int"),
//    })
    @PostMapping("deleteShip")
    @ApiOperation("取消管理员所对应的权限")
    public Result deleteShip(@RequestBody List<UserIdAndPermissionIdVo> userIdAndPermissionIdVoList){
        this.adminPermissionService.deleteShip(userIdAndPermissionIdVoList);
        return Result.success("");
    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pId", value = "权限的id", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "sId", value = "机构id", required = true, dataType = "int")})
    @ApiOperation("查找机构权限下对应的人员")
    @GetMapping("userForPermissionVos")
    public Result userForPermissionVos(int pId,int sId){
        List<UserForPermissionVo> userForPermissionVos = adminPermissionService.selectUserForRoleAndStore(pId, sId);
        return Result.success(userForPermissionVos,"");

    }
    @ApiOperation("用戶登录成功的权限菜单")
    @GetMapping("selectLoginSuccessMenu")
    public Result selectLoginSuccessMenu(HttpServletRequest request){
        Integer userId = Integer.parseInt(request.getHeader("userId"));
//        Integer userId =Integer.parseIntclaims.get("userId").toString());=2;
        List<PermissionMenuVo> permissionMenuVos = adminPermissionService.selectLoginSuccessMenu(userId);
        return Result.success(permissionMenuVos,"");

    }


}
